package com.ebt.m.proposal_v2.exempt_rule;

import android.widget.LinearLayout;

import com.ebt.m.proposal_v2.bean.InsuranceEntity;
import com.ebt.m.proposal_v2.ui.view.InsuranceView;
import com.ebt.m.proposal_v2.widget.view.InsuranceCellView;

import java.util.ArrayList;
import java.util.List;

/**
 * 豁免险查找类
 *
 * @author archer.qi
 *         Created on 2017/9/19.
 */
public class ExemptViewSearcher {
    private InsuranceView bindView;
    private LinearLayout bindViewContainer;

    public ExemptViewSearcher(InsuranceView view) {
        this.bindView = view;
        if (view != null) {
            this.bindViewContainer = view.getContainer();
        }
    }

    /**
     * 获取[豁免类型为投保人]的所有[豁免险View]
     *
     * @return
     */
    public List<InsuranceCellView> getExemptViewsForApplicant() {
        if (bindViewContainer == null) {
            throw new IllegalArgumentException("bindViewContainer is null");
        }
        List<InsuranceCellView> mExemptionCellViews = new ArrayList<>();
        int childCount = bindViewContainer.getChildCount();

        for (int i = 0; i < childCount; i++) {
            InsuranceCellView cellView = (InsuranceCellView) bindViewContainer.getChildAt(i);
            if (cellView != null) {
                InsuranceEntity cellData = cellView.getData();
                if (cellData != null && cellData.isExemption() && cellData.getExemptionType() == 1) { // 是豁免险 && 豁免投保人
                    mExemptionCellViews.add(cellView);
                }
            }
        }
        return mExemptionCellViews;
    }

    /**
     * 获取[豁免指定附加险]的所有[豁免险]
     *
     * @param data [指定][普通]附加险
     * @return
     */
    public List<InsuranceCellView> getExemptViews(InsuranceEntity data) {
        if (bindViewContainer == null) {
            throw new IllegalArgumentException("bindViewContainer is null, please set a valid InsuranceView");
        }

        List<InsuranceCellView> mExemptionCellViews = new ArrayList<>();
        int childCount = bindViewContainer.getChildCount();

        for (int i = 0; i < childCount; i++) {
            InsuranceCellView cellView = (InsuranceCellView) bindViewContainer.getChildAt(i);
            if (cellView != null) {
                InsuranceEntity cellData = cellView.getData();
                if (cellData != null && cellData.isExemption()) {
                    // 是豁免险
                    List<String> cellExemptions = cellData.exemptProduct;

                    if (cellExemptions != null && cellExemptions.contains(data.planCode)) {
                        // 豁免当前产品
                        mExemptionCellViews.add(cellView);
                    }
                    if (cellExemptions != null && data != null && data.isMain() && cellExemptions.contains("*")) {
                        // 如果是主险，且豁免主险
                        mExemptionCellViews.add(cellView);
                    }
                }
            }
        }
        return mExemptionCellViews;
    }

    /**
     * 获取所有豁免险
     *
     * @return
     */
    public List<InsuranceCellView> getAllExemptViews() {
        if (bindViewContainer == null) {
            throw new IllegalArgumentException("bindViewContainer is null, please set a valid InsuranceView");
        }

        List<InsuranceCellView> mExemptionCellViews = new ArrayList<>();
        int childCount = bindViewContainer.getChildCount();

        for (int i = 0; i < childCount; i++) {
            InsuranceCellView cellView = (InsuranceCellView) bindViewContainer.getChildAt(i);
            if (cellView != null) {
                InsuranceEntity cellData = cellView.getData();
                if (cellData != null && cellData.isExemption()) { // 是豁免险
                    mExemptionCellViews.add(cellView);
                }
            }
        }
        return mExemptionCellViews;
    }

    /**
     * 获取所有产品的数据
     *
     * @return
     */
    public List<InsuranceEntity> getAllProductsData() {
        if (bindViewContainer == null) {
            throw new IllegalArgumentException("bindViewContainer is null, please bind a valid InsuranceView first");
        }
        return bindView.getProductsData();
    }

    /**
     * 获取最后一项CellView
     *
     * @return
     */
    public InsuranceCellView getLastCellView() {
        return (InsuranceCellView) bindViewContainer.getChildAt(bindViewContainer.getChildCount() - 1);
    }

    /**
     * 检查Container是否为空
     *
     * @return
     */
    public boolean checkContainerNull() {
        return bindViewContainer == null || (bindViewContainer != null && bindViewContainer.getChildCount() <= 0);
    }


}
